package homework;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2023-01-06
 * Time: 21:18
 */
import java.util.*;
//https://www.nowcoder.com/practice/d77d11405cc7470d82554cb392585106?tpId=13&&tqId=11174&rp=1&ru=/activity/oj&qru=/ta/coding-interviews/question-ranking
public class Solution {
    public boolean IsPopOrder(int [] pushA,int [] popA) {
        int n = pushA.length;
        Deque<Integer> stack = new LinkedList<>();
        int k = 0;
        for (int i = 0; i < n; i++) {
            while (k < n && (stack.isEmpty() || stack.peek() != popA[i])) {
                stack.push(pushA[k]);
                k++;
            }
            if (stack.peek() == popA[i]) {
                stack.pop();
            } else {
                return false;
            }
        }
        return true;
    }
}



import java.util.*;

public class Solution {
    public boolean IsPopOrder(int [] pushA,int [] popA) {
        Deque<Integer> stack = new LinkedList<>();
        int len1 = pushA.length;
        int k = 0;
        for (int i = 0; i < len1; i++) {
            stack.push(pushA[i]);
            while (!stack.isEmpty() && stack.peek() == popA[k]) {
                stack.pop();
                k++;
            }
        }
        while (!stack.isEmpty()) {
            if (stack.peek() == popA[k]) {
                k++;
                stack.pop();
            } else {
                return false;
            }
        }
        return true;
    }
}